Entity Relationship Diagram: illustrates how entities relate to eachother within a system.
Look at this sample: Entity Relationship Diagram.pdf
Partial DDL implementation of this ERD using SQLite:
CREATE TABLE Article ( ArticleCode INTEGER NOT NULL UNIQUE PRIMARY KEY AUTOINCREMENT, Headline CHAR(50) NOT NULL, AuthorEmail CHAR(128) NOT NULL, ContentPayload TEXT NOT NULL, Metatags TEXT ); CREATE TABLE Websites ( URL CHAR(128) NOT NULL UNIQUE PRIMARY KEY, PlatformName CHAR(50) NOT NULL ); CREATE TABLE Distribution ( ArticleCode INTEGER NOT NULL, URL CHAR(128) NOT NULL, PRIMARY KEY (ArticleCode, URL) );
Or to enforce ERD constraints apply following SQLite foreign keys to child tables, in this case Distribution table:
CREATE TABLE Distribution ( ArticleCode INTEGER NOT NULL, URL CHAR(128) NOT NULL, PRIMARY KEY (ArticleCode, URL) FOREIGN KEY (ArticleCode) REFERENCES Article (ArticleCode) ON DELETE CASCADE ON UPDATE CASCADE FOREIGN KEY (URL) REFERENCES Websites (URL) ON DELETE CASCADE ON UPDATE CASCADE );
Try following UPDATE and DELETE queries after creating the Distribution child table with foreign key constraints enabled:
populate
INSERT INTO Article (Headline, AuthorEmail, ContentPayload) VALUES ("Hi!", "jo@jo.com", "<h1>Hello World</h1>"); INSERT INTO Websites (URL, PlatformName) VALUES ("facebook.com", "Facebook"); INSERT INTO Websites (URL, PlatformName) VALUES ("youtube.com", "YouTube"); INSERT INTO Distribution (ArticleCode, URL) VALUES (1, "youtube.com");manipulate
UPDATE Websites SET URL = "m.facebook.com" WHERE URL == "facebook.com"; DELETE FROM Websites WHERE PlatformName == "YouTube";